package com.xidian.infrastructure.persistent.repository;

import com.xidian.domain.strategy.model.entity.StrategyAwardsEntity;
import com.xidian.domain.strategy.repository.IStrategyRepository;
import com.xidian.infrastructure.persistent.dao.IStrategyAwardsDao;
import com.xidian.infrastructure.persistent.po.StrategyAwardsPO;
import com.xidian.infrastructure.redis.IRedisService;
import com.xidian.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StrategyRepository implements IStrategyRepository {

    @Resource
    private IStrategyAwardsDao strategyAwardsDao;

    @Resource
    private IRedisService redisService;

    @Override
    public List<StrategyAwardsEntity> queryStrategyAwardsList(Long strategyId) {
        //优先从缓存中获取
        String catchKey = Constants.RedisKey.STRATEGY_AWARD_KEY + strategyId;
        List<StrategyAwardsEntity> strategyAwardsEntities = redisService.getValue(catchKey);
        if (null != strategyAwardsEntities && !strategyAwardsEntities.isEmpty()) {
            return strategyAwardsEntities;
        }
        //从库中读取数据
        List<StrategyAwardsPO> strategyAwards = strategyAwardsDao.queryStrategyListByStrategyId(strategyId);
        strategyAwardsEntities = new ArrayList<>(strategyAwards.size());
        for (StrategyAwardsPO strategyAwardsPO : strategyAwards) {
            StrategyAwardsEntity strategyAwardsEntity = StrategyAwardsEntity.builder()
                    .awardsId(strategyAwardsPO.getAwardsId())
                    .awardsStrategyId(strategyAwardsPO.getAwardsStrategyId())
                    .awardsCount(strategyAwardsPO.getAwardsCount())
                    .awardsCountSurplus(strategyAwardsPO.getAwardsCountSurplus())
                    .awardsProbability(strategyAwardsPO.getAwardsProbability())
                    .build();
            strategyAwardsEntities.add(strategyAwardsEntity);
        }
        redisService.setValue(catchKey, strategyAwardsEntities);
        return strategyAwardsEntities;
    }

    @Override
    public void storeStrategyAwardsProbabilityTable(Long strategyId, int size, Map<Integer, Integer> shuffleStrategyAwardsSearchProbabilityTable) {
        //存储抽奖策略范围值
        redisService.setValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + strategyId, size);
        //存储概率查找表 - 存在则删除重新装配
        String tableCacheKey = Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + strategyId;
        if (redisService.isExists(tableCacheKey)) {
            redisService.remove(tableCacheKey);
        }
        Map<Integer, Integer> catchProbabilityTable = redisService.getMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + strategyId);
        catchProbabilityTable.putAll(shuffleStrategyAwardsSearchProbabilityTable);
    }

    @Override
    public int getProbabilityRange(Long strategyId) {
        return redisService.getValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + strategyId);
    }

    @Override
    public Integer getStrategyAwardsAssemble(Long strategyId, int probabilityKey) {
        return redisService.getFromMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + strategyId, probabilityKey);
    }
}
